package org.i1510.common.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;


/**
 * @author Administrator
 * conf.properties 文件读取
 */
public class ConfPropertiesUtils {
//	 private String path = "/config/common/conf.properties";
//	private String path = "D:\\java\\workspace\\ekpEIP2013\\src\\com\\landray\\kmss\\joeone\\salary\\conf.properties";

	public Properties loadPro(String path) {
		Properties prop = new Properties();// 属性集合对象
		FileInputStream fis;
		try {
			fis = new FileInputStream(ConfPropertiesUtils.class.getResource("/").getPath()+path);
			// 属性文件输入流
			prop.load(fis);// 将属性文件流装载到Properties对象中
			fis.close();// 关闭流

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return prop;
	}

	/**
	*@Author Rambo
	*@Desc：更新配置资源文件
	*@param path 路径
	*@param map 更新数据
	*@param userName 更新人
	*@return TODO
	*@Version  ConfPropertiesUtils.java,v 1.1 2014-11-4 下午4:51:40 
	*/
	public Properties updatePro(String path,Map<String, String> map,String userName) {
		Properties prop = new Properties();// 属性集合对象
		FileInputStream fis;
		try {
			fis = new FileInputStream(ConfPropertiesUtils.class.getResource("/").getPath()+path);
			// 属性文件输入流
			prop.load(fis);// 将属性文件流装载到Properties对象中
			fis.close();// 关闭流

			// 修改sitename的属性值
			Iterator it = map.entrySet().iterator();

			while (it.hasNext()) {

				Map.Entry entry = (Map.Entry) it.next();
				prop.setProperty(entry.getKey().toString().trim(), entry
						.getValue().toString());

			}

	
			// 文件输出流
			FileOutputStream fos = new FileOutputStream(ConfPropertiesUtils.class.getResource("/").getPath()+path);
			// 将Properties集合保存到流中
			prop.store(fos, "Copyright (c) "+userName+" "+DateUtils.getDate(0));
			fos.close();// 关闭流
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return prop;
	}

	/**
	 * @author kenshinbo@163.com 功能：
	 * @param args
	 * @version $Id: PropertiesUtils.java,v 1.1 2013-11-7 下午02:54:56 Exp $
	 */
	public static void main(String[] args) {

		ConfPropertiesUtils pUtils = new ConfPropertiesUtils();

		Properties pro = pUtils.loadPro("/config/common/conf.properties");
		System.out.println("获取属性值：path=" + pro.getProperty("email.find.pw"));
		Map<String,String> map = new HashMap<String,String>();
		map.put("password", "123456");
		pUtils.updatePro("/config/common/conf.properties",map, "kenshinbo");
	}

}
